package edu.cuc.chap02.lianxi.l2.Server;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.Date;

public class TimeServerHandlerl2 extends ChannelInboundHandlerAdapter {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(ctx.channel().id());
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf request = (ByteBuf) msg;
        byte[] message = new byte[request.readableBytes()];
        request.readBytes(message);
        String orderMessage = new String(message);
        String res = "QUERY TIME ORDER".equalsIgnoreCase(orderMessage) ? new Date().getTime() + "" : "BAD ODER";
        System.out.println(orderMessage);
        byte[] responseMessage = res.getBytes();
        ByteBuf response = Unpooled.copiedBuffer(responseMessage);
        ctx.writeAndFlush(response);
    }
}
